<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>阳光成单系统</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<link rel="stylesheet" href="lib/layui/layui/css/layui.css">
<script src="lib/layui/layui/layui.js"></script>
</head>
<body>
	<div class="x-nav">
		<span class="layui-breadcrumb"> <a><cite>首页</cite></a> <a><cite>会员管理</cite></a>
			<a><cite>分类管理</cite></a>
		</span> <a class="layui-btn layui-btn-small"
			style="line-height: 1.6em; margin-top: 3px; float: right"
			href="javascript:location.replace(location.href);" title="刷新"><i
			class="layui-icon" style="line-height: 30px">ဂ</i></a>
	</div>
	<div id="r_div" style="display: none">
		<form class="layui-form" action="" id="d_form" lay-filter="form_mov">
			<div class="layui-form-item">
				<label class="layui-form-label">阅览室名称</label>
				<div class="layui-input-block">
					<input type="text" name="name" lay-verify="title"
						autocomplete="off" placeholder="请输入阅览室名称" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-input-block">
					<button type="submit" class="layui-btn" lay-submit=""
						lay-filter="tijiao">立即提交</button>
					<button type="reset" class="layui-btn layui-btn-primary">重置</button>
				</div>
			</div>
		</form>
	</div>
	<div id="r_div1" style="display: none">
		<form class="layui-form" action="" id="d_form1" lay-filter="form_mov1">
			<div class="layui-inline">
				<label class="layui-form-label">id</label>
				<div class="layui-input-inline" >
					<input type="text" name="id" autocomplete="off"
						class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">阅览室名称</label>
				<div class="layui-input-block">
					<input type="text" name="name" lay-verify="title"
						autocomplete="off" placeholder="请输入阅览室名称" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-input-block">
					<button type="submit" class="layui-btn" lay-submit=""
						lay-filter="tijiao1">立即提交</button>
					<button type="reset" class="layui-btn layui-btn-primary">重置</button>
				</div>
			</div>
		</form>
	</div>
	<!-- 进入资源室 -->
	<div id="into_div" style="display: none">
		<form class="layui-form" action="" id="into_form" lay-filter="into_form">
			<div class="layui-inline">
				<label class="layui-form-label">请输入一个学号</label>
				<div class="layui-input-inline" style="width: 100px;">
					<input type="text" id="into_id" name="into_id" autocomplete="off"
						class="layui-input">
				</div>
			</div>
			<div class="layui-inline">
				<div class="layui-input-inline" style="width: 100px;">
					<input type="text" id="intoInput" autocomplete="off" disabled
					 style="border:none;outline:none;text-align:center;" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-input-block">
					<button type="submit" class="layui-btn" lay-submit=""
						lay-filter="into_into">立即进入</button>
				</div>
			</div>
		</form>
	</div>
	<!-- 离开资源室 -->
	<div id="out_div" style="display: none">
		<form class="layui-form" action="" id="out_form" lay-filter="out_form">
			<div class="layui-inline">
				<div class="layui-input-iblack">
					<input type="text" id="out_id" name="out_id" autocomplete="off"
						class="layui-input">
				</div>
				<label class="layui-form-label" style="width: 150px">您确定要离开资源室吗</label>
			</div>
			<div class="layui-inline">
				<div class="layui-input-inline" style="width: 100px;">
					<input type="text" id="outInput" autocomplete="off" disabled
					 style="border:none;outline:none;text-align:center;" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-input-block">
					<button type="submit" class="layui-btn" lay-submit=""
						lay-filter="out_but">立即退出</button>
				</div>
			</div>
		</form>
	</div>
	<button type="submit" class="layui-btn" lay-submit="" lay-filter="add">添加</button>
	<table id="demo" lay-filter="test"></table>
	<script type="text/html" id="barDemo">
  		<a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
		<a class="layui-btn layui-btn-sm" lay-event="into">进入</a>
		<a class="layui-btn layui-btn-sm" lay-event="out">离开</a>
		<a class="layui-btn layui-btn-sm" lay-event="del">删除</a>
	</script>
	<script>
	var intoid;
	var intoName;
	layui.use(['table','form','layer'], function(){
	  var table = layui.table;
			var form =layui.form;
			var layer=layui.layer;
			var $=layui.$;
	  //第一个实例
	  table.render({
	    elem: '#demo'
	    ,id:'roomTab'
	    ,url: 'fandAllRoom' //数据接口
		,parseData: function (res) {
                return {
                    "code": 0
                    ,"msg": "暂无数据"
                    , "data": res
                }
            }
	    ,cols: [[ //表头
	      {field: 'id', title: 'ID', sort: true, fixed: 'left'}
	      ,{field: 'name', title: '阅览室'},{fixed: 'right', align:'center', toolbar: '#barDemo'}
	    ]]
	  });
	   form.on('submit(add)', function(data){
				    layer.open({
				    	type:1,
				    	content:$("#r_div"),
				    	area:["400px","400px"]
				    });
					return false;

				  });
	  //添加
		form.on('submit(tijiao)', function(data){
				  $.post("insertById",$("#d_form").serialize(),function(res){
					  if (res.code == '0') {
						layer.closeAll();
						table.reload("roomTab");
					} else {
						layer.msg(res.msg);
					}
					  
				  },"json");
					return false;
			  });
			  
			  // 进入资源室
			  form.on('submit(into_into)', function(data){
			  	  var intos = $("#into_id").val();
			  	  console.log(intos);
			  	  $.post("selMemberByCard_nos",{card_no:intos},function(res){
					  if (res.code == '0') {
					  $("#intoInput").val("您现在可以进入");
					  	$.post("addConsume_log",{read_room:intoid,member:intos},function(res){
							  if (res.code == '0') {
								$.post("updateStatusInById",{status:intoName, id:intos},function(res){
									  if (res.code == '0') {
										layer.closeAll();
										table.reload("roomTab");
									} else {
										layer.msg(res.msg);
									}
								  },"json");
							} else {
								layer.msg(res.msg);
							}
						  },"json");
					} else {
						$("#intoInput").val("您现在还不能可以进入,请重置你的状态");
						layer.msg(res.msg);
					}
				  },"json");
				  
					return false;
			  });
			  
			  // 离开资源室
			  form.on('submit(out_but)', function(data){
			  	  var intos = $("#out_id").val();
			  	  console.log(intos);
			  	  $.post("selByMemberAndRead_room",{read_room_id:intoid, card_no:intos},function(res){
					  if (res.code == '0') {
					  	var cId = res.data.id;
					  	var cn = res.data.member.card_no;
					  	var rId = res.data.read_room.id;
					  	var mId = res.data.member.id;
					  	console.log("cId"+cId);
					  	console.log("intoid"+intoid);
					  	console.log("rId"+rId);
					  	console.log("cn"+cn);
					  	console.log("mId"+mId);
					  	if(rId==intoid){
						  	$.post("updateConsume_logById",{id:cId},function(res){
								  if (res.code == '0') {
								  	$.post("selMemberByCard_no",{card_no:cn},function(res){
										  if (res.code == '0') {
											$.post("updateStatusOutById",{id:mId},function(res){
												  if (res.code == '0') {
													layer.closeAll();
													table.reload("roomTab");
												} else {
													layer.msg(res.msg);
												}
											  },"json");
										} else {
											layer.msg(res.msg);
										}
									  },"json");
								} else {
									layer.msg(res.msg);
								}
							  },"json");
					  	} else {
					  		$("#outInput").val("您需要退出的资源室编号不对");
					  	}
					  	
					} else {
						$("#outInput").val("您现在还不能可以离开,因为你还没有进入一个资源室或者学号错误");
						layer.msg(res.msg);
					}
				  },"json");
				  
					return false;
			  });
			  
			  
			  //监听工具条 (编辑)
				  table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
				    var data = obj.data; //获得当前行数据
				    var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
				    var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
				   
				    if (layEvent === 'del') { //删除
			layer.confirm('真的删除行么', function(index) {
				$.post("delRoom", {
					id : data.id
				}, function(res) {
					if (res.success) {
						layer.msg(res.msg, {
						icon : 1,
						time : 2000
						});
					$(obj).parents("tr").remove();
					} else {
						layer.msg(res.msg, {
						icon : 1,
						time : 2000
						});
					}
					table.reload("roomTab");
				}, "json");
			});
			} else if(layEvent === 'edit'){ //编辑
				    	form.val("form_mov1",data);
				    	layer.open({
					    	type:1,
					    	content:$("#r_div1"),
					    	area:["400px","400px"]
					    });
				    } else if(layEvent === 'into'){ //进入
				    	intoid = data.id;
				    	intoName = data.name;
				    	layer.open({
					    	type:1,
					    	content:$("#into_div"),
					    	area:["400px","400px"]
					    });
				    } else if(layEvent === 'out'){ //退出
				    	intoid = data.id;
				    	layer.open({
					    	type:1,
					    	content:$("#out_div"),
					    	area:["400px","400px"]
					    });
				    }
				     
					return false;
				  });
				//编辑
				  form.on('submit(tijiao1)', function(data){
					  $.post("updateRoom",$("#d_form1").serialize(),function(res){
						  if (res.code == '0') {
							layer.closeAll();
							table.reload("roomTab");
						} else {
							layer.msg(res.msg);
						}
					  },"json");
					  return false;
				  });
});
</script>
</body>
</html>